package cn.dakaqi.vo.response;

import cn.dakaqi.entities.Activity;
import cn.dakaqi.entities.ActivitySignRecord;
import cn.dakaqi.entities.user.Volunteer;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author chunyang on 2016/5/5.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ActivitySignRecordVO implements Serializable
{
    private static final long serialVersionUID = -7249015617667258710L;
    /**
     * ********志愿者信息**************
     */
    private String nickName;//昵称
    private String realName;//真实姓名
    private String headUrl;//头图

    /**
     * **********活动信息*************************************
     */
    private String activityName;            //活动名称
    private String activityStartTime;
    private String activityEndTime;
    private String activityAddress;
    private Double activityLng;
    private Double activityLat;
    private int activityScope;
    /**
     * ************************签到/签退时间********************
     */
    private int times;
    private String inTime;
    private String outTime;

    public static ActivitySignRecordVO buildVO(Volunteer volunteer, Activity activity, List<ActivitySignRecord> list)
    {
        ActivitySignRecordVO vo = new ActivitySignRecordVO();
        vo.setActivityLng(activity.getLng());
        vo.setActivityLat(activity.getLat());
        vo.setRealName(volunteer.getRealName());
        vo.setNickName(volunteer.getNickName());
        vo.setHeadUrl(volunteer.getHeadUrl());
        vo.setActivityName(activity.getName());
        vo.setActivityStartTime(activity.getStartTime());
        vo.setActivityEndTime(activity.getEndTime());
        vo.setActivityAddress(activity.getAddress());
        vo.setActivityScope(activity.getScope());
        if (null == list || list.size() == 0 || list.isEmpty())
        {
            vo.setInTime("");
            vo.setOutTime("");
        } else
        {
            vo.setInTime(DateUtil.DefaultTimeFormatter.format(list.get(0).getCreateTime()));

            try
            {
                if (list.size() >= 2)
                {
                    vo.setOutTime(DateUtil.DefaultTimeFormatter.format(list.get(list.size() - 1).getCreateTime()));
                    if (activity.getActStatus() >= DKQConstant.ACT_STATUS_PROCESS)
                    {
                        //vo.setTimes(DateUtil.countMinutes2(vo.getInTime(), vo.getOutTime()));

                        //若第一次签到时间在活动开始之前则是：最后一次签到时间-活动的开始时间；若第一次签到时间在活动开始之后，则是最后一次签到时间-第一次签到时间
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        Date firstSign = list.get(0).getCreateTime();
                        Date lastSign = list.get(list.size() - 1).getCreateTime();
                        Date actStartDate = sdf.parse(activity.getStartTime());
                        int tagetTimes = DateUtil.countMinutes(activity.getStartTime(), activity.getEndTime());
                        if(firstSign.before(actStartDate))
                        {
                            int times = DateUtil.countMinutes(activity.getStartTime(), sdf.format(lastSign));
                            if(times>=tagetTimes)
                                times = tagetTimes;
                            vo.setTimes(times);
                        }
                        else
                        {
                            int times = DateUtil.countMinutes(sdf.format(firstSign), sdf.format(lastSign));
                            if(times>=tagetTimes)
                                times = tagetTimes;
                            vo.setTimes(times);
                        }

                    }
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        return vo;
    }
}
